package com.song.demo14springbootmybatisplus;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import java.util.ArrayList;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;


/**
 * 代码生成器
 */
public class CodeGenertateBuilder {
    public static void main(String[] args) {
        // 构建一个代码自动生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();

        // 1.全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); //输出路径
        globalConfig.setFileOverride(false); //是否覆盖
        globalConfig.setOpen(false);
        globalConfig.setEnableCache(false);
        globalConfig.setAuthor("宋大米");
        globalConfig.setSwagger2(true);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setServiceName("%Service"); //去掉Service的I前缀
        globalConfig.setIdType(IdType.ASSIGN_ID);

        autoGenerator.setGlobalConfig(globalConfig);

        // 2.设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl("jdbc:mysql://192.168.37.117:8899/tn_mdm_test?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("mdm_test");
        dataSourceConfig.setPassword("test007");
        autoGenerator.setDataSource(dataSourceConfig);

        // 3.包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.song");
        packageConfig.setModuleName("user");
        packageConfig.setEntity("entity");
        packageConfig.setService("service");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        autoGenerator.setPackageInfo(packageConfig);

        // 4.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        // TODO: 2023/9/13 需要生成不同的表的时候，只需要改这里名字，及上面包名
        strategyConfig.setInclude("sys_role","sys_menu");  //设置要映射的表名,可以设置多张生成代码的表
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);  //下划线命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true); //自动lombok
        strategyConfig.setLogicDeleteFieldName("deleted");  //逻辑删除字段
        // 自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategyConfig.setTableFillList(tableFills);
        // 乐观锁
        strategyConfig.setVersionFieldName("version");

        autoGenerator.setStrategy(strategyConfig);


        // 执行生成代码
        autoGenerator.execute();

    }

}
